#include "widget.h"
#include <QTouchEvent>
#include <QPainter>
#include <QDebug>

Widget::Widget(QWidget *parent) : QWidget(parent)
{
    setAttribute(Qt::WA_AcceptTouchEvents);
}

Widget::~Widget()
{

}

bool Widget::event(QEvent *ev)
{
    if (ev->type() == QEvent::TouchBegin) {
        ev->accept();
    } else if (ev->type() == QEvent::TouchUpdate) {
    } else if (ev->type() == QEvent::TouchEnd) {
    } else if (ev->type() == QEvent::TouchCancel) {
    } else {
        return QWidget::event(ev);
    }

    if (!touchPoints.isEmpty()) {
        for (auto ii : touchPoints) {
            update(ii);
        }
        touchPoints.clear();
    }

    QTouchEvent *tev = (QTouchEvent *) ev;
    for (auto ii : tev->touchPoints()) {
        auto rect = ii.rect().toRect();
        touchPoints.append(rect);
        update(rect);
    }

    return true;
}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::green);
    painter.drawRects(touchPoints);
}

